package com.liugan.magic.map;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.loon.framework.android.game.core.LSystem;
import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.opengl.GLEx;
import org.loon.framework.android.game.core.graphics.opengl.LTexture;
import org.loon.framework.android.game.core.resource.Resources;

import android.util.Log;

import com.liugan.magic.Constants;
import com.liugan.magic.actor.Butterfly;
import com.liugan.magic.actor.Fox;
import com.liugan.magic.actor.Frog;
import com.liugan.magic.actor.Key;
import com.liugan.magic.actor.Rabbit;
import com.liugan.magic.actor.still.Door;
import com.liugan.magic.actor.still.Grass;
import com.liugan.magic.actor.still.Stone;
import com.liugan.magic.screen.PointXY;
import com.liugan.magic.screen.layer.GameLayer;
import com.liugan.magic.utils.ImagesUtils;

public class Magicmap {

	public static final int TILE_SIZE = 16;
	public int row, col,width,height;
	private char[][] actormap;
	private char[][] imagemap;
	public PointXY totalXY ,offsetXY;
	
	public Magicmap(int gamecode,String actorfile,String imagefile,GameLayer layer,PointXY totalXY ,PointXY offsetXY) {
		this.totalXY=totalXY;
		this.offsetXY=offsetXY;
		// 加载游戏地图
		load(gamecode,actorfile,imagefile,layer);
		// PS:地图读取时行列顺序颠倒,此时开始col,row作用相反
		width = TILE_SIZE * col;
		height = TILE_SIZE * row;
	}
	/**
	 * 背景
	 * 32 290
	 * @param g 
	 * @param offsetX
	 * @param offsetY
	 */
	public void draw(GLEx g, int offsetX, int offsetY) {
		
		for (int i = 0; i < 1; i++) {
			for (int j = 0; j < col; j++) {
				
				int tilesj=tilesToPixels(j);
				int tilesi=tilesToPixels(i);
				switch (imagemap[i][j]) {
				//B C D E F
				case 'B':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().bg1,"bg1")){
					g.drawTexture(ImagesUtils.getinstance().bg1, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'C':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().bg2,"bg2")){
					g.drawTexture(ImagesUtils.getinstance().bg2, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'D':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().bg3,"bg3")){
					g.drawTexture(ImagesUtils.getinstance().bg3, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'E':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().bg4,"bg4")){
					g.drawTexture(ImagesUtils.getinstance().bg4, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'F':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().bg5,"bg5")){
					g.drawTexture(ImagesUtils.getinstance().bg5, tilesj - offsetX,tilesi - offsetY);}
					break;
				}
			}
		}

	}
	/**
	 * 前景
	 * 32 290
	 * @param g 
	 * @param offsetX
	 * @param offsetY
	 */
	public void drawFore(GLEx g, int offsetX, int offsetY) {
		for (int i = 1; i < row; i++) {
			for (int j = 0; j < col; j++) {
				int tilesj=tilesToPixels(j);
				int tilesi=tilesToPixels(i);
				switch (imagemap[i][j]) {
				//B C D E F
				case 'b':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().fg1,"fg1")){
					g.drawTexture(ImagesUtils.getinstance().fg1, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'c':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().fg2,"fg2")){
					g.drawTexture(ImagesUtils.getinstance().fg2, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'd':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().fg3,"fg3")){
					g.drawTexture(ImagesUtils.getinstance().fg3, tilesj - offsetX,tilesi - offsetY);}
					break;
				case 'e':
					if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().fg4,"fg4")){
					g.drawTexture(ImagesUtils.getinstance().fg4, tilesj - offsetX,tilesi - offsetY);}
					break;
					// 草地填充物
				case 'f':
					//if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().grass_1,"fg1"))
					g.drawTexture(ImagesUtils.getinstance().grass_g, tilesj - offsetX,tilesi - offsetY);
					break;
					// 草地
				case 'g':
					//if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().grass_1,"fg1"))
					g.drawTexture(ImagesUtils.getinstance().grass_1, tilesj - offsetX,tilesi - offsetY);
					break;
					//最后的草
				case 'h':
					//if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().grass_2,"fg1"))
					g.drawTexture(ImagesUtils.getinstance().grass_2, tilesj - offsetX,tilesi - offsetY);
					break;
					//石头1
				case 'i':
					//if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().grass_2,"grass_2")){
					g.drawTexture(ImagesUtils.getinstance().grass_2, tilesj - offsetX,tilesi - offsetY);
					break;
					//石头2
				case 'j':
					//if (isinscreen(tilesj - offsetX,tilesi - offsetY,ImagesUtils.getinstance().grass_2,"grass_2")){
					g.drawTexture(ImagesUtils.getinstance().grass_2, tilesj - offsetX,tilesi - offsetY);
					break;
				}
			}
		}

	}
	
	/**
	 * 加载游戏地图
	 * 
	 * @param filename
	 */
	public void load(int gamecode,String actorfile,String imagefile,GameLayer layer) {
		
		//树木
		//动物(青蛙)
		//狐狸
		//蝴蝶
		//钥匙
		//草地
		try {
			// @ TODO 使用assert下的文件 :Resources.openResource(resName)
			//BufferedReader br = new BufferedReader(new InputStreamReader(Magicmap.class.getResourceAsStream(actorfile)));
			BufferedReader br = new BufferedReader(new InputStreamReader(Resources.openResource("assets/maps/"+gamecode+"/actormap.data")));
			// 地图行数
			String line = br.readLine();
			row = Integer.parseInt(line);
			// 地图列数
			line = br.readLine();
			col = Integer.parseInt(line);
			actormap = new char[row][col];
			imagemap = new char[row][col];
			
			
			// 将地图加载入数组
			
			for (int i = 0; i < row; i++) {
				line = br.readLine();
				for (int j = 0; j < col; j++) {
					actormap[i][j] = line.charAt(j);
					PointXY totlexy=new PointXY(tilesToPixels(j),tilesToPixels(i));
					switch (actormap[i][j]) {
					// 石头
					case 's':
						layer.addObject(new Stone(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
					// 蝴蝶
					case 'b':
						layer.addObject(new Butterfly(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
					// 兔子
					case 'r':
						layer.addObject(new Rabbit(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
					//青蛙
					case 'f':
						layer.addObject(new Frog(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
					//狐狸
					case 'e':
						layer.addObject(new Fox(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
						// 草地
					case 'g':
						layer.addObject(new Grass(totlexy,totlexy,""),totlexy.x,totlexy.y);
						break;
						//最后的草
					case 'h':
						layer.addObject(new Grass(totlexy,totlexy,"end"),totlexy.x,totlexy.y);
						break;
						//门
					case 'd':
						layer.addObject(new Door(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
						//门
					case 'k':
						layer.addObject(new Key(totlexy,totlexy),totlexy.x,totlexy.y);
						break;
					}
				}
			}
			br.close();
			
			//读图片map
			//BufferedReader imagebr = new BufferedReader(new InputStreamReader(Magicmap.class.getResourceAsStream(imagefile)));
			BufferedReader imagebr = new BufferedReader(new InputStreamReader(Resources.openResource("assets/maps/"+gamecode+"/imagemap.data")));
			imagebr.readLine();//越过两行
			imagebr.readLine();
			for (int i = 0; i < row; i++) {
				line = imagebr.readLine();
				for (int j = 0; j < col; j++) {
					imagemap[i][j] = line.charAt(j);
				}
			}
			imagebr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
		}
	}
	
	
	/**
	 * 像素坐标转换为地图坐标
	 * 
	 * @param pixels
	 * @return
	 */
	public static int pixelsToTiles(double pixels) {
		return (int) Math.floor(pixels / TILE_SIZE);
	}

	/**
	 * 地图坐标转换为像素坐标
	 * 
	 * @param tiles
	 * @return
	 */
	public static int tilesToPixels(int tiles) {
		return tiles * TILE_SIZE;
	}
	
	/** 判断是在屏幕内
	 * @param offx
	 * @return
	 */
	public boolean isinscreen(int offx,int offy,LTexture limage,String iname){
		if ((offx>=0&&offx<=Constants.ScreenWIDTH)||((offx+Constants.maximagewidth)>=0&&(offx+Constants.maximagewidth)<=Constants.ScreenWIDTH)||(offx<=0&&(offx+Constants.maximagewidth)>=Constants.ScreenWIDTH)){
			if (limage==null){
				
				ImagesUtils.getinstance().getLTexture(iname);
			}
			return true;
		}else{
			if (limage!=null){
				ImagesUtils.getinstance().disLTexture(iname);
			}
			return false;
		}
	}
}
